#ifndef _RFID_INTERFACE_C_
#define _RFID_INTERFACE_C_

#include "include/zkrd522.h"
#include "include/iso14443a.h"
#include "include/rfid_interface.h"
#include "include/spi.h"
#include "include/adc.h"
#include "include/Beep.h"

unsigned char ADC_GetRefValue_FirstTime = 1;

char get_typeA_card(unsigned char *cln, unsigned char *uid, unsigned char *sak_tmp)
{
    unsigned char  uid_tmp[4],sak,tag_type;
    int  tmp = MI_EMPTY;
    tmp = pcd_request(PICC_REQALL,&tag_type);

    if((tmp == MI_OK) || (tmp == MI_COLL))
    {
        tmp = pcd_anticoll(PICC_ANTICOLL1, uid_tmp);
        if(tmp == MI_OK)
        {
						uid[0] = uid_tmp[0];
						uid[1] = uid_tmp[1];
						uid[2] = uid_tmp[2];
						uid[3] = uid_tmp[3];
            tmp = pcd_select(PICC_ANTICOLL1, uid_tmp, &sak);
            
            if(tmp == MI_OK)
            {
                *sak_tmp = sak;

                if(sak & 0x04)                                        //通过sak字节bit3来判断接收的UID字节是否完整
                {
                    tmp = pcd_anticoll(PICC_ANTICOLL2, uid_tmp);        //CLN2

                    if(tmp == MI_OK)
                    {
                        tmp = pcd_select(PICC_ANTICOLL2, uid_tmp, &sak);

                        if(tmp == MI_OK)
                        {
                            if(sak & 0x04)
                            {
                                tmp = pcd_anticoll(PICC_ANTICOLL3, uid_tmp);//CLN3

                                if(tmp == MI_OK)
                                {
                                    tmp = pcd_select(PICC_ANTICOLL3, uid_tmp, &sak);

                                    if(tmp == MI_OK)
                                    {
                                        if(sak & 0x04)
                                        {
                                            return PROTOCOL_ERR;
                                        }
                                        else
                                        {
                                            *cln = 3;                        //级联为3，即10字节长UID
                                            uid[8] = uid_tmp[0];
                                            uid[9] = uid_tmp[1];
                                            uid[10] = uid_tmp[2];
                                            uid[11] = uid_tmp[3];

                                            return MI_OK;
                                        }
                                    }
                                    else
                                    {
                                        return MI_COLLERR;
                                    }
                                }
                                else
                                {
                                    return MI_COLLERR;
                                }
                            }
                            else
                            {
                                *cln = 2;//级联为2,7字节UID
                                uid[4] = uid_tmp[0];
                                uid[5] = uid_tmp[1];
                                uid[6] = uid_tmp[2];
                                uid[7] = uid_tmp[3];

                                return MI_OK;
                            }
                        }
                        else
                        {
                            return MI_COLLERR;
                        }
                    }
                    else
                    {
                        return MI_COLLERR;
                    }
                }
                else
                {
                    *cln = 1;//级联为1,4字节UID
                    uid[0] = uid_tmp[0];
                    uid[1] = uid_tmp[1];
                    uid[2] = uid_tmp[2];
                    uid[3] = uid_tmp[3];

                    return MI_OK;
                }
            }
            else
            {
                return MI_COLLERR;
            }
        }
        else
        {
            return MI_COLLERR;
        }
    }
    else
    {

        return MI_NOTAGERR;
    }
	return tmp;
}


void ScanRFID(void)
{
	unsigned char  uid[12];
	unsigned char cln,sak; 
	
	static unsigned int AD_CH1Value = 0,AD_CH1Value_tmp = 0;
	static unsigned int AD_CH3Value = 0,AD_CH3Value_tmp = 0;

	int status = MI_NOTAGERR;
	
	RD522_RST_PIN = 1;                     //RST拉高使能
	Delay_50us(24);												//延迟1.2ms以上
	spi_write_reg(analogen_reg, 0x00);   	//关闭内部vmid电路
	spi_write_reg(command_reg, 0x00);   	//关闭内部aldo电路
	spi_write_reg(testpinen_reg, 0x00);
	spi_write_reg(txcontrol_reg, 0x1b);		//开启天线TX1;bit4->100%;bit0->TX1EN
	if(ADC_GetRefValue_FirstTime == 1)		//获取无卡片时的天线载波幅度参考值
	{
		P17 = 0;
		P20 = 1;
		ADC_GetRefValue_FirstTime = 0;
		AD_CH1Value = Get_AdcValue(ADC_CH1);	
		AD_CH3Value = Get_AdcValue(ADC_CH3);
	}
	else
	{
		AD_CH1Value_tmp = Get_AdcValue(ADC_CH1);
		AD_CH3Value_tmp = Get_AdcValue(ADC_CH3);

		if((AD_CH1Value_tmp < (AD_CH1Value-50))||  (AD_CH3Value_tmp < (AD_CH3Value-50)))//一般来说，天线载波幅度发生变化，说明有卡靠近
		{
			P17 = 0;//单独绿灯提示误触发
			P20 = 1;
			spi_write_reg(analogen_reg, 0x0f);//打开模拟相关电路
			spi_write_reg(command_reg, 0x80);//打开模拟电路开关
			pcd_config('A');
			pcd_set_rate(RATE_106,10);
			Delay_ms(100);
			status = get_typeA_card(&cln,uid,&sak);
			if(MI_OK == status)
			{
				RD522_RST_PIN = 0; 
				BEEP_ON;
				P17 = 1;
				P20 = 0;
				Delay_ms(100);
				BEEP_OFF;
				P17 = 0;
				P20 = 1;
				Delay_ms(100);
				P17 = 1;
				P20 = 1;
			}
			else
			{
			}
		}
		else
		{
			P17 = 1;
			P20 = 1;
		}
	}
	
}

#endif
